package com.centerm.bluetooth.jf;

import android.content.Context;
import android.support.annotation.NonNull;

import com.alibaba.fastjson.JSONObject;
import com.centerm.bluetooth.common.controller.PBOCController;
import com.centerm.util.HexUtil;
import com.centerm.util.Logger;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

/**
 * Created by linwenhui on 2017/5/8.
 */

public class JFPBOCController extends PBOCController {
    public JFPBOCController(@NonNull Context context) {
        super(context);
    }

    @Override
    protected void parsePBOCResult(byte[] params) {
        if (params[0] == 0 || params[0] == 0x03) {
            JSONObject resultObj = new JSONObject();
            //随机数
            byte[] randomBytes = new byte[8];
            System.arraycopy(params, 1, randomBytes, 0, 8);
            resultObj.put("random", HexUtil.byte2HexStr(randomBytes));

            //等效二磁道数据密文长度
            int tradeLen = params[9] & 0xFF;
            if (tradeLen != 0x00 && tradeLen != 0xFF) {
                byte[] tradeBytes = new byte[tradeLen];
                System.arraycopy(params, 10, tradeBytes, 0, tradeLen);
                resultObj.put("trade", HexUtil.byte2HexStr(tradeBytes));
            } else {
                resultObj.put("trade", "");
            }

            //IC卡数据密文长度
            byte[] icLenByte = new byte[2];
            System.arraycopy(params, 10 + tradeLen, icLenByte, 0, 2);
            int icLen = 0;
            try {
                icLen = Integer.valueOf(HexUtil.byte2HexStr(icLenByte), 16);
            } catch (NumberFormatException e) {
                e.printStackTrace();
            }
            Logger.i(" icLen == " + icLen);

            //IC卡数据密文信息
            if (icLen > 0) {
                byte[] icDataBytes = new byte[icLen];
                System.arraycopy(params, 12 + tradeLen, icDataBytes, 0, icLen);
                resultObj.put("icdata", HexUtil.byte2HexStr(icDataBytes));
            } else {
                resultObj.put("icdata", "");
            }

            //卡号明文数据
            byte[] panBytes = new byte[10];
            System.arraycopy(params, 12 + tradeLen + icLen, panBytes, 0, 10);
            resultObj.put("pan", HexUtil.byte2HexStr(panBytes).replace("F", "").replace("f", ""));


            //卡有效期
            byte[] expireDateBytes = new byte[4];
            System.arraycopy(params, 22 + tradeLen + icLen, expireDateBytes, 0, 4);
            resultObj.put("expireDate", HexUtil.toASCII(expireDateBytes));

            Logger.i(resultObj.toJSONString());

            //持卡人姓名
            int nameLen = params[26 + tradeLen + icLen] & 0xFF;
            if (nameLen > 0 && nameLen != 0xFF) {
                byte[] nameBytes = new byte[nameLen];
                System.arraycopy(params, 27 + tradeLen + icLen, nameBytes, 0, nameLen);
                try {
                    resultObj.put("name", new String(nameBytes, "GBK"));
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                    resultObj.put("name", new String(nameBytes));
                }
            }

            //卡序列号
            byte[] cardSerailBytes = new byte[3];
            System.arraycopy(params, 27 + tradeLen + icLen + nameLen, cardSerailBytes, 0, 3);
            resultObj.put("cardSerail", HexUtil.toASCII(cardSerailBytes));

            if (baseListener != null) {
                baseListener.onPBOCResult(resultObj.toJSONString());
            }


        } else {
            try {
                endPBOC(1);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
